﻿using AutoDeployForSims.Entity;
using AutoDeployForSims.Model;
using AutoDeployForSims.Service;
using AutoDeployForSims.Utility;
using AutoDeployForSims.View;
using MyUtility.Utility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;
using static System.Net.Mime.MediaTypeNames;

namespace AutoDeployForSims.ViewModel
{
    public class AllStatusDisplayControlViewModel : ViewModelBase
    {
        //查询线程
        private StatusCheckService statusCheckService = SingletonProvider<StatusCheckService>.Instance;
        private CallOutAppModel callOutAppModel = SingletonProvider<CallOutAppModel>.Instance;


        #region ViewModel

        private ObservableCollection<StatusDisplayControlViewModel> statusPanelItems = new ObservableCollection<StatusDisplayControlViewModel>();
        public ObservableCollection<StatusDisplayControlViewModel> StatusPanelItems
        {
            get { return statusPanelItems; }
            set
            {
                statusPanelItems = value;
                RaisePropertyChanged("StatusPanelItems");
            }
        }

        
        #endregion

        public AllStatusDisplayControlViewModel()
        {
            AppInfoModel appInfoModel = SingletonProvider<AppInfoModel>.Instance;
            foreach (AppStatusEntity item in appInfoModel.AppStatusEntities)
            {
                StatusDisplayControlViewModel cardViewModel = new StatusDisplayControlViewModel(item);
                statusPanelItems.Add(cardViewModel);
            }
        }
        
        public void UpdateStatusPanel(AppStatusEntity appStatusEntity)
        {
            StatusDisplayControlViewModel cardViewModel = new StatusDisplayControlViewModel(appStatusEntity);
            statusPanelItems.Add(cardViewModel);
        }
        public void DeleteStatusPanel(AppStatusEntity appStatusEntity)
        {
            int num = statusPanelItems.Count() - 1;
            for (int i = num; i > 0; i--)
            {
                if (appStatusEntity == statusPanelItems[i].AppStatusEnt)
                {
                    statusPanelItems.Remove(statusPanelItems[i]);
                }                
            }            
        }

        public void StartAllAPP()
        {
            foreach (var item in statusPanelItems)
            {
                callOutAppModel.ShowApp(item.AppStatusEnt.AppName, item.AppStatusEnt.AppPath);
            }
        }
        public void StopAllAPP()
        {
            foreach (var item in statusPanelItems)
            {
                callOutAppModel.CloseApp(item.AppStatusEnt.AppName);
            }
        }

        public void Start()
        {
            statusCheckService.StartCheckThread();
        }
    }
}
